package demo;

import com.alibaba.fastjson.JSON;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.*;

/**
 * @Author wuchao66
 * @Date 2023/5/23 10:25
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@WebServlet(name = "Testphone",urlPatterns = "/Testphone")
public class Testphone extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        String phone = request.getParameter("phone");
        //手机号码 +验证码
        String host = "http://edisim.market.alicloudapi.com";
        String path = "/comms/sms/groupmessaging";
        String method = "POST";
        String appcode = "6752ec311a2a4066862ad4a3b374c1dc";
        //封装请求
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("callbackUrl", "http://www.esandinfo.com/sms/notify");
        bodys.put("mobileSet", "["+phone+"]");
        bodys.put("templateID", "20230414140359");
        int num= new Random().nextInt(900000)+100000;
        String y=String.format("%s,%s",num,3);
        //String x=String.format("%s,%s",num,5);
        // String y= MessageFormat.format("{0}{1}");
        bodys.put("templateParamSet", y);
        System.out.println(num);
        //bodys.put("templateParamSet", x);
        // System.out.println(y);
        //redis session
//     HttpSession session = request.getSession();
//       session.setAttribute(phone,num);//保存好验证码
//       session.setMaxInactiveInterval(180);
//       System.out.println("保存验证码成功，收到的验证码是"+session.getAttribute(phone));
        try {
           System.out.println("xxx");
             HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println("yyy");
     } catch (Exception e) {
          e.printStackTrace();
       }
//        System.out.println(phone);
//      String num=  SendSms.sendSms(phone);
//        System.out.println(num);
//        HttpSession session = request.getSession();
//        session.setMaxInactiveInterval(180);
//        session.setAttribute(phone,num);
        out.print(JSON.toJSONString(num));
        out.flush();
        out.close();
    }





//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter out = response.getWriter();//文件流
//
//        out.flush();
//        out.close();
//    }


}
